package com.scitc.pleasurevideo;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.util.AtyContainer;
import com.scitc.pleasurevideo.x5webview.X5WebView;
import com.tencent.smtt.export.external.interfaces.WebResourceError;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author 853
 */
public class WebviewPlayActivity extends ETActivity {
    private X5WebView mX5WebView;
    private ProgressBar progressBar;
    private RelativeLayout titleBar;
    private TextView bannerTitle;
    private RelativeLayout loginlayout;
    private View blackView;
    private TextView progressText;


    private Timer timer = new Timer();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
                loadPostJs();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_webview_play);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) actionBar.hide();


        String playUrl = getIntent().getStringExtra("playUrl");
        String playName = getIntent().getStringExtra("playName");

        mX5WebView = (X5WebView) findViewById(R.id.x5WebView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        titleBar = (RelativeLayout) findViewById(R.id.titleBar);
        bannerTitle = (TextView) findViewById(R.id.bannerTitle);
        loginlayout = (RelativeLayout) findViewById(R.id.loginlayout);
        progressText = (TextView) findViewById(R.id.progressText);
        blackView = (View) findViewById(R.id.blackView);

        bannerTitle.setText(playName);


        mX5WebView.loadUrl(playUrl);

        initX5WebView();
        setProgressBar();
        setClick();
        loadJs();
    }

    private void loadJs() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 0;
                handler.sendMessage(message);
            }
        }, 0, 1000);
    }


    private void loadPostJs() {
        String js = "$(\".dplayer-full\").remove();";
        mX5WebView.loadUrl("javascript:" + js);
    }

    /**
     * 启用硬件加速
     */
    private void initX5WebView() {
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getWindow()
                        .setFlags(
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                                android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }
    }

    /**
     * 设置进度条
     */
    private void setProgressBar() {
        mX5WebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //显示进度条
                if (newProgress < 100) {
                    progressText.setText(newProgress + "");
                    progressBar.setVisibility(View.VISIBLE);
                    progressText.setVisibility(View.VISIBLE);
                    blackView.setVisibility(View.VISIBLE);
                } else {
                    progressBar.setVisibility(View.GONE);
                    progressText.setVisibility(View.GONE);
                    blackView.setVisibility(View.GONE);
                }
            }
        });
    }

    /**
     * 显示头部bar,然后一秒隐藏
     */
    private void setTitleBarVisible() {
        loginlayout.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                loginlayout.setVisibility(View.GONE);
            }
        }, 1000);

    }


    /**
     * 点击事件
     */
    private void setClick() {
        mX5WebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
                //点击事件
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                //加载错误
            }
        });

        titleBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setTitleBarVisible();
            }
        });

    }

    /**
     * 返回键监听
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mX5WebView != null && mX5WebView.canGoBack()) {
                mX5WebView.goBack();
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        //释放资源
        if (mX5WebView != null)
            mX5WebView.destroy();
        timer.cancel();
        super.onDestroy();
    }
}